<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/14/014
 * Time: 15:07
 */

namespace App\Http\Controllers\Admin;

use Illuminate\Support\Facades\Request;

class BaseAdminController extends \App\Http\Controllers\Controller
{
	public function __construct()
	{

	}

	/**
	 * 获取表单数据
	 * @param $name
	 * @param null $default
	 * @param string $filter
	 * @return array|mixed|null|string
	 */
	public function input($name, $default = null, $filter = '')
	{
		$value = \request()->input($name, $default);
		if ($filter) {
			$value = $this->filterValue($value, $default, $filter);
		}
		return $value;
	}

	/**
	 * 表单数据过滤
	 * @param $value
	 * @param $default
	 * @param $filter
	 * @return mixed
	 */
	private function filterValue($value, $default, $filter)
	{
		/*if (is_scalar($value)) {
			if (is_callable($filter)) {
				$value = call_user_func($filter, $value);
			} elseif (strpos($filter, '/')) {
				//正则验证
				if (!preg_match($filter, $value)) {
					$value = $default;
				}
			} else {
				$value = $default;
			}
		} else {
			foreach ($value as $key => $v) {
				$temp = $this->filterValue($v, $default, $filter);
				$value[$key] = $temp;
			}
		}*/

		if (is_callable($filter)) {
			$value = call_user_func($filter, $value);
		} elseif (strpos($filter, '/')) {
			//正则验证
			if (!preg_match($filter, $value)) {
				$value = $default;
			}
		} else {
			$value = $default;
		}
		return $value;
	}

}